import { defineStore } from 'pinia'
import { ConfigService } from "../../bindings/app/service/index";
import i18n from '../locales'

const changeTheme = (theme: string) => {
    document.body.setAttribute('arco-theme', theme)
}
const changeLocale = (value: any) => {
    i18n.global.locale = value
}
export const useConfigStore = defineStore('config', {
    state: () => ({
        locale: "zh",
        theme: "light",
    }),
    getters: {
    },
    actions: {
        set(key: string, value: string) {
            this.$patch({ [key]: value })
            if (key === 'locale') {
                changeLocale(value)
            } else if (key === 'theme') {
                changeTheme(value)
            }
            this.SaveConf()
        },
        setConfig(cfg: any) {
            cfg.theme = cfg.theme || "light";
            cfg.locale = cfg.locale || "zh";
            this.$patch(cfg);
            changeTheme(cfg.theme);
            changeLocale(cfg.locale);
        },
        SaveConf() {
            ConfigService.Set(this.$state);
        },
    },
})